using System;
using System.Collections.Generic;
using Slqj.Common;
using Shc.Sql;
namespace Slqj.Data
{
    [Serializable]
    public class Administrators
        : BaseAdministrators, ILoginInfo
    {
        #region 通用操作
        /// <summary>
        /// 新增数据
        /// </summary>
        /// <returns></returns>
        public bool Insert()
        {
            using (DbSession ss = DbManager.Current.OpenSession())
            {

                return DbManager.Current.Insert(ss, this);
            }
        }

        /// <summary>
        /// 新增数据,使用事务
        /// </summary>
        /// <param name="ss">数据库session</param>
        /// <returns></returns>
        public bool Insert(DbSession ss)
        {
            return DbManager.Current.Insert(ss, this);
        }

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <returns></returns>
        public bool Update()
        {
            using (DbSession ss = DbManager.Current.OpenSession())
            {

                return DbManager.Current.Update(ss, this);
            }
        }

        /// <summary>
        /// 更新实体,使用事务
        /// </summary>
        /// <param name="ss">数据库session</param>
        /// <returns></returns>
        public bool Update(DbSession ss)
        {
            return DbManager.Current.Update(ss, this);
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public bool Delete()
        {
            using (DbSession ss = DbManager.Current.OpenSession())
            {
                return DbManager.Current.Delete(ss, this);
            }
        }

        /// <summary>
        /// 删除实体,使用事务
        /// </summary>
        /// <param name="ss">数据库session</param>
        /// <returns></returns>
        public bool Delete(DbSession ss)
        {
            return DbManager.Current.Delete(ss, this);
        }

        /// <summary>
        /// 根据条件批量删除
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public static bool Delete(Criterion[] criteria)
        {
            using (DbSession ss = DbManager.Current.OpenSession())
            {
                return DbManager.Current.DeleteEntities(ss, TableAdministrators.Table, criteria);
            }
        }

        /// <summary>
        /// 根据条件使用事务批量删除
        /// </summary>
        /// <param name="ss">数据库session</param>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public static bool Delete(DbSession ss, Criterion[] criteria)
        {
            return DbManager.Current.DeleteEntities(ss, TableAdministrators.Table, criteria);
        }

        /// <summary>
        /// 根据条件查询单个实体
        /// </summary>
        /// <returns></returns>
        public static Administrators Find(params Criterion[] criteria)
        {
            using (DbSession ss = DbManager.Current.OpenSession())
            {
                return DbManager.Current.Find<Administrators>(ss, criteria);
            }
        }

        /// <summary>
        /// 根据条件查询单个实体
        /// </summary>
        /// <returns></returns>
        public static Administrators Find(DbSession ss, params Criterion[] criteria)
        {
            return DbManager.Current.Find<Administrators>(ss, criteria);
        }

        /// <summary>
        /// 根据主键查询实体
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public static Administrators FindByPK(params object[] keys)
        {
            using (DbSession ss = DbManager.Current.OpenSession())
            {
                return DbManager.Current.FindPK<Administrators>(ss, keys);
            }
        }

        /// <summary>
        /// 根据主键查询实体
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public static Administrators FindByPK(DbSession ss, params object[] keys)
        {
            return DbManager.Current.FindPK<Administrators>(ss, keys);
        }

        /// <summary>
        /// 根据条件查询
        /// </summary>
        /// <param name="orders"></param>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public static IList<Administrators> FindAll(Order[] orders, params Criterion[] criteria)
        {
            IList<Administrators> list = new List<Administrators>();
            using (DbSession ss = DbManager.Current.OpenSession())
            {
                DbManager.Current.Query(ss, list, orders, criteria);
                return list;
            }
        }

        /// <summary>
        /// 根据条件查询
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public static IList<Administrators> FindAll(params Criterion[] criteria)
        {
            return FindAll(new Order[0], criteria);
        }

        /// <summary>
        /// 根据条件查询
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public static IList<Administrators> FindAll(DbSession ss, params Criterion[] criteria)
        {
            return FindAll(new Order[0], ss, criteria);
        }

        /// <summary>
        /// 根据条件查询
        /// </summary>
        /// <param name="orders"></param>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public static IList<Administrators> FindAll(Order[] orders, DbSession ss, params Criterion[] criteria)
        {
            IList<Administrators> list = new List<Administrators>();

            DbManager.Current.Query(ss, list, orders, criteria);
            return list;
        }

        /// <summary>
        /// 根据条件分页查询
        /// </summary>
        /// <param name="orders"></param>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public static IList<Administrators> FindByPage(int curPage, int pagesize, Order[] orders,
            params Criterion[] criteria)
        {
            IList<Administrators> list = new List<Administrators>();
            using (DbSession ss = DbManager.Current.OpenSession())
            {
                DbManager.Current.Query(ss, list, curPage, pagesize, orders, criteria);
                return list;
            }
        }

        /// <summary>
        /// 根据条件分页查询
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public static IList<Administrators> FindByPage(int curPage, int pagesize, params Criterion[] criteria)
        {
            return FindByPage(curPage, pagesize, new Order[0], criteria);
        }

        /// <summary>
        /// 根据条件分页查询
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public static IList<Administrators> FindByPage(int curPage, int pagesize,
            DbSession ss, params Criterion[] criteria)
        {
            return FindByPage(curPage, pagesize, new Order[0], ss, criteria);
        }

        /// <summary>
        /// 根据条件分页查询
        /// </summary>
        /// <param name="orders"></param>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public static IList<Administrators> FindByPage(int curPage, int pagesize, Order[] orders,
            DbSession ss, params Criterion[] criteria)
        {
            IList<Administrators> list = new List<Administrators>();

            DbManager.Current.Query(ss, list, curPage, pagesize, orders, criteria);
            return list;
        }
        #endregion

        #region ILoginInfo 成员

        int m_loginType;
        /// <summary>
        /// 登录类型
        /// </summary>
        public int LoginType
        {
            get { return m_loginType; }
            set { m_loginType = value; }
        }

        string m_loginName;
        /// <summary>
        /// 登录名
        /// </summary>
        public string LoginName
        {
            get { return m_loginName; }
            set { m_loginName = value; }
        }

        public void SaveData(ArcByteSet arcSet)
        {
            arcSet.Set(LoginType);
            arcSet.Set(this.AdministratorID);
            arcSet.Set(this.LoginID);
            arcSet.Set(this.LoginName);
        }

        public void ReadData(ArcByteGet arcGet)
        {
            LoginType = arcGet.GetInt();
            this.AdministratorID = arcGet.GetInt();
            this.LoginID = arcGet.GetString();
            this.LoginName = arcGet.GetString();
        }

        #endregion


        /// <summary>
        /// 检查登录帐号是否存在
        /// </summary>
        /// <param name="loginid"></param>
        /// <returns></returns>
        public static bool CheckLoginID(string loginid)
        {
            using (DbSession ss = DbManager.Current.OpenSession())
            {
                Administrators admin = DbManager.Current.Find<Administrators>(ss,
                                                                              Criterion.Eq(TableAdministrators.LoginID,
                                                                                           loginid));
                if (admin == null)
                {
                    return false;
                }
                return true;
            }
        }

        /// <summary>
        /// 获取帐号对应的角色列表
        /// </summary>
        /// <param name="adminstratorsId"></param>
        /// <returns></returns>
        public static string[] GetAdminRoles(int adminstratorsId)
        {
            AuthAdminRole adminRole =
                AuthAdminRole.Find(Criterion.Eq(TableAuthAdminRole.AdministratorID, adminstratorsId));
            return adminRole.GetRoles();
        }

        public static string[] GetAdminPermissions(int administratorsId)
        {
            string[] roleList = Administrators.GetAdminRoles(administratorsId);
            IList<AuthRoles> authRoleses = AuthRoles.FindAll(new Order[] { Order.Asc(TableAuthRoles.RoleID) },
                                                             Criterion.In(TableAuthRoles.RoleID, roleList));
            List<string> permissionCodeList = new List<string>();
            foreach (AuthRoles authRolese in authRoleses)
            {
                string[] permissions = authRolese.GetPermissionList();
                foreach (string permission in permissions)
                {
                    if (!permissionCodeList.Contains(permission))
                    {
                        permissionCodeList.Add(permission);
                    }
                }
            }
            return permissionCodeList.ToArray();
        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="administrators">用户对应的账户信息</param>
        /// <returns></returns>
        public int CreateAdmin(Administrators administrators, int roleId)
        {
            int result = -1;
            //该登录帐号已存在
            if (Administrators.CheckLoginID(administrators.LoginID))
            {
                return 1;
            }
            using (DbTransCounter trans = new DbTransCounter(DbManager.Current.OpenSession(), true).BeginTransaction())
            {
                AuthRoles roles = AuthRoles.FindByPK(roleId);
                if (roles != null)
                {
                    administrators.Insert(trans.Session);
                    //this.AdministratorID = administrators.AdministratorID;
                    //this.Insert(trans.Session);

                    AuthAdminRole adminRole = new AuthAdminRole();
                    adminRole.AdministratorID = this.AdministratorID;
                    adminRole.RoleIDList = roleId.ToString();

                    adminRole.Insert(trans.Session);
                }

                trans.Commit();
                result = 0;
            }
            return result;
        }


        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="administrators">用户对应的账户信息</param>
        /// <returns></returns>
        public int CreateAdmin(Administrators administrators, int roleId, DbSession ss)
        {
            int result = -1;
            //该登录帐号已存在
            if (Administrators.CheckLoginID(administrators.LoginID))
            {
                return 1;
            }
            AuthRoles roles = AuthRoles.FindByPK(roleId);
            if (roles != null)
            {
                administrators.Insert(ss);
                //this.AdministratorID = administrators.AdministratorID;
                //this.Insert(ss);

                AuthAdminRole adminRole = new AuthAdminRole();
                adminRole.AdministratorID = this.AdministratorID;
                adminRole.RoleIDList = roleId.ToString();

                adminRole.Insert(ss);
            }
            result = 0;
            return result;
        }

    }
}
